﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Restbucks.Shared.Representations;
using Restbucks.Shared.Domain;
using Restbucks.Shared.Mappers;
using Restbucks.Server.Activities;
using System.Xml.Linq;
using System.Xml;
using System.Xml.Serialization;
using Restbucks.Utilities;

namespace Restbucks.Server.Resources
{
    public class OrderResource
    {
        //public OrderRepresentation getOrder()
        //{
        //    try {
        //        OrderRepresentation responseRepresentation = new ReadOrderActivity()
        //}

        private readonly CreateOrderActivity _createOrderActivity;
        private readonly ReadOrderActivity _readOrderActivity;
        private readonly RemoveOrderActivity _removeOrderActivity;

        public OrderResource()
        {
            _createOrderActivity = (CreateOrderActivity) RestbucksActivityFactory.GetInstance(typeof(CreateOrderActivity));
            _removeOrderActivity = (RemoveOrderActivity) RestbucksActivityFactory.GetInstance(typeof(RemoveOrderActivity));
            _readOrderActivity = (ReadOrderActivity) RestbucksActivityFactory.GetInstance(typeof(ReadOrderActivity));
        }



        public XDocument CreateOrder(XDocument request)
        {
            //try
            //{
                Debug.Log(request.ToString());

                OrderRepresentation orderRep = Utilities.Serialization.Deserialize<OrderRepresentation>(request);
                //                Order order = OrderRepresentationMapper.GetDomainObject(orderRep);

                OrderRepresentation orderResponse = _createOrderActivity.Create(orderRep, new Uri("http://something"));


                XDocument response = Utilities.Serialization.Serialize(orderResponse);
                Debug.Log(response.ToString());
                return response;
            //}
            //catch (Exception e)
            //{
            //    Debug.Log(e);
            //    throw e;
            //}
            //return null;
        }


        public XDocument GetOrders()
        {
            OrderRepresentation[] orders = _readOrderActivity.ReadOrders();
            XDocument response = Serialization.Serialize(orders);
            return response;
        }
    }
}
